<?php
/**
 * Created by PhpStorm.
 * User: admin
 * Date: 2016/10/18
 * Time: 11:57
 */

function getSysConfig()
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");
    $sql = "select * from yg_shop_config limit 1";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}

function getDefProduct()
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");
    $sql = "select * from yg_products where products_index=1";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}


function addUserInfo($data)
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");

    $product_id = $data['product_id'];

    $order_sn = $data['order_sn'];
    $order_sn = "'$order_sn'";

    $num = $data['num'];
    $num = "'$num'";

    $user_name = $data['user_name'];
    $user_name = "'$user_name'";

    $email = $data['email'];
    $email = "'$email'";

    $addr = $data['addr'];
    $addr = "'$addr'";

    $phone = $data['phone'];
    $phone = "'$phone'";

    $date = $data['add_date'];
    $date = "'$date'";
    $sql = "replace into yg_userinfo(`product_id`,`order_sn`,`num`,`user_name`,`phone`,`email`,`addr`,`add_date`) values ($product_id,$order_sn,$num,$user_name,$phone,$email,$addr,$date)";
    $res = $dbh->exec($sql);
    return $res;
}


function lastedInsertId()
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");

    $date = time();
    $sql = "insert into auto_increment_sn(`createtime`) values ($date)";
    $id = $dbh->exec($sql);
    return $dbh->lastInsertId();
}


function getKdInfo($sn)
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");
    $sql = "select * from yg_kd where order_sn='$sn'";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}


function getProductById($pid)
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");
    $sql = "select * from yg_products where products_id='$pid'";
    $sth = $dbh->query($sql);
    $row = $sth->fetch();
    return $row;
}


function updateProducts($pid, $data)
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");
    $str = '';
    $sign = ',';
    $i = 0;
    foreach ($data as $key => $v) {
        $i++;
        $str .= "`$key`" . "=" . $v;
        if ($i == count($data)) {
            $sign = '';
        }
    }
    $sql = "update yg_products set $str where products_id='$pid'";
    $ret = $dbh->exec($sql);
    return $ret;
}


function saveEmailData($data)
{
    $dsn = "mysql:dbname=" . DBNAME . ";host=" . SERVER . ";port=" . PORT;
    $dbh = new PDO($dsn, DBUSER, PWD);
    $dbh->query("set names utf8");

    $product_id = $data['product_id'];
    $product_id = "'$product_id'";

    $order_sn = $data['order_sn'];
    $order_sn = "'$order_sn'";

    $to_email = $data['to_email'];
    $to_email = "'$to_email'";

    if (isset($data['createtime'])) {
        $createtime = $data['createtime'];
        $modifytime = time();
        $sql = "replace into yg_mailinfo(`product_id`,`order_sn`,`to_email`,`createtime`,`modifytime`) values ($product_id,$order_sn,$to_email,$createtime,$modifytime)";
    } else {
        $modifytime = time();
        $sql = "replace into yg_mailinfo(`product_id`,`order_sn`,`to_email`,`modifytime`) values ($product_id,$order_sn,$to_email,$modifytime)";
    }


    $res = $dbh->exec($sql);
    return $res;
}